from util.base_driver import driver
from selenium.common.exceptions import  NoSuchElementException
import  os
from util.log import  Log
import  time

log = Log()

def get_screenshot():
    root_path = os.path.dirname(os.path.dirname(__file__))
    screen_path = os.path.join(root_path, 'screenshots')
    if not os.path.exists(screen_path):
        os.mkdir(screen_path)
    return screen_path


def take_screenshot():
    screen_path = get_screenshot()
    run_path = os.path.join(screen_path, 'run')
    if not os.path.exists(run_path):
        os.mkdir(run_path)
    file_name = time.strftime('%Y_%m_%d_%H_%M_%S')
    file_path = os.path.join(run_path,file_name+'.png')
    driver.save_screenshot(file_path)


def handle_error(errorfilename):
    # 如果出现异常，在一个专门的目录里面截屏
    screen_path = get_screenshot()
    error_path = os.path.join(screen_path,'run_error')
    if not os.path.exists(error_path):
        os.mkdir(error_path)
    file_path = os.path.join(error_path,errorfilename+'.png')
    driver.save_screenshot(file_path)

